<?php

    /**
     * @author AbordageSol
     * @version 1.0
     * @created 03-Mar-2012 12:22:41 PM
     */
    class Booking {

        var $_ci;

        function Booking ()
        {
            $this->_ci = & get_instance ();
        }

        /**
         * 
         * @param session
         * return row carbooking
         */
        function AddToBooking ( $session )
        {

            // First add customer to db table customers
            $customer_data = array (
                'firstName' => urldecode ( $session['cust_name'] ),
                'email' => urldecode ( $session['cust_email'] ),
                'phone' => urldecode ( $session['cust_phone'] ),
                'address1' => urldecode ( $session['cust_address1'] ),
                'town_city' => urldecode ( $session['cust_town_city'] ),
                'zip_code' => urldecode ( $session['cust_zip_code'] ),
                'country' => urldecode ( $session['cust_country'] ),
                'what' => 2
            );

            // Checkfor customer
            $this->_ci->db->where ( array ('email' => $customer_data['email'], 'what' => $customer_data['what']) );
            $this->_ci->db->from ( 'customers' );
            $res = $this->_ci->db->get ()->result_array ();

            // Customer double check
            //if (count($res)==0){
            $this->_ci->db->insert ( 'customers', $customer_data );
            $cust_id = $this->_ci->db->insert_id ();
            //}else{
            //    $cust_id = $res[0]['id'];
            //}
            //booking status
            switch ( $session['source_info'] )
            {

                case 'www.novisajt.com':
                    $status = 5;
                    $hotel = '';
                    $roomnumber = '';
                    break;

                case 'www.informacionisistem.com':
                    $status = 0;
                    $hotel = urldecode ( $session['cust_hotel'] );
                    $roomnumber = urldecode ( $session['cust_roomnumber'] );
                    break;

                case 'www.budva.com':
                    $status = 5;
                    $hotel = '';
                    $roomnumber = '';
                    break;

                default:
                    $status = 5;
                    $hotel = '';
                    $roomnumber = '';
            }


            $carbooking_data = array (
                'datefrom' => strtotime ( $session['pickup_date'] . ' ' . $session['fullfrom'] ),
                'dateto' => strtotime ( $session['return_date'] . ' ' . $session['fullto'] ),
                'carid' => $session['car_id'],
                'pickup_loc' => $session['pickup_loc_text'],
                'return_loc' => $session['return_loc_text'],
                'numofdays' => $session['no_days'],
                'totalprice' => $session['tot_price'],
                'dayprice' => $session['day_price'],
                'pickup_loc_price' => $session['pickup_loc_value'],
                'return_loc_price' => $session['return_loc_value'],
                'source_info' => $session['source_info'],
                'customerid' => $cust_id,
                'user_id' => 7,
                'status' => $status,
                'note' => urldecode ( $session['cust_note'] ),
                'hotel' => $hotel,
                'roomnumber' => $roomnumber
            );
            $this->_ci->db->insert ( 'carbooking', $carbooking_data );
            $cb_id = $this->_ci->db->insert_id ();

            // Add accessories to db table cbaccessories
            if ( isset ( $session['extras'] ) )
            {
                foreach ( $session['extras'] as $extra_id )
                {
                    $extra = array (
                        'carBookingId' => $cb_id,
                        'adId' => $extra_id
                    );
                    $this->_ci->db->insert ( 'cbaccessories', $extra );
                }
            }

            $res = $this->_ci->db->get_where ( 'carbooking', array ('id' => $cb_id) );
            return $res;
        }

        /**
         * 
         * @param booking_id
         * @param car_id
         */
        function ShowOrder_1 ( $booking_id )
        {
            if ( !isset ( $booking_id ) )
            {
                echo 'Error ocured.<br /> booking_id is not set.';
                exit ();
            }

            $query = '
            SELECT cb.*, cust.firstName, cust.email , cust.phone, cust.address1, cust.town_city, cust.zip_code, cust.country, car.id as carid, car.name as carname
            FROM carbooking as cb
            LEFT JOIN customers as cust ON cb.customerid  = cust.id
            LEFT JOIN car ON cb.carid = car.id
            WHERE cb.id = ' . $booking_id . '
            ';

            $res = $this->_ci->db->query ( $query );

            return $res;
        }

        function ShowOrder_2 ( $booking_id )
        {
            if ( !isset ( $booking_id ) )
            {
                echo 'Error ocured.<br /> booking_id is not set.';
                exit ();
            }

            $query = '
            SELECT cbac.*, acd.type, acd.description , acd.price
            FROM cbaccessories as cbac
            LEFT JOIN accessorydescription as acd ON cbac.adId  = acd.id
            WHERE cbac.carBookingId = ' . $booking_id . '
            ';

            $res = $this->_ci->db->query ( $query );

            return $res;
        }

        function ShowTransaction ( $trans_id )
        {

            if ( !isset ( $trans_id ) )
            {
                echo 'Error ocured.<br /> trans_id is not set.';
                exit ();
            }

            $res = $this->_ci->db->get_where ( 'transaction', array ('trans_id' => $trans_id) );
            return $res;
        }

        function FindType ( $trans_id )
        {

            if ( !isset ( $trans_id ) )
            {
                echo 'Error ocured.<br /> trans_id is not set.';
                exit ();
            }

            $this->_ci->db->where ( 'trans_id', $trans_id );
            $this->_ci->db->from ( 'excursionbooking' );
            $cnt = $this->_ci->db->count_all_results ();
            if ( $cnt > 0 )
                return 'excursionbooking';

            $this->_ci->db->where ( 'trans_id', $trans_id );
            $this->_ci->db->from ( 'tourbooking' );
            $cnt = $this->_ci->db->count_all_results ();
            if ( $cnt > 0 )
                return 'tourbooking';

            $this->_ci->db->where ( 'trans_id', $trans_id );
            $this->_ci->db->from ( 'carbooking' );
            $cnt = $this->_ci->db->count_all_results ();
            if ( $cnt > 0 )
                return 'carbooking';

            return "error";
        }

        function FindId ( $trans_id, $type )
        {

            if ( !isset ( $trans_id ) )
            {
                echo 'Error ocured.<br /> trans_id is not set.';
                exit ();
            }
            if ( !isset ( $type ) )
            {
                echo 'Error ocured.<br /> booking_type is not set.';
                exit ();
            }

            return $this->_ci->db->where ( 'trans_id', $trans_id )->get ( $type );
        }

        function UpdateStatus ( $type, $id )
        {

            switch ( $type )
            {

                case 'carbooking':
                    $this->_ci->db->where ( 'id', $id )->update ( 'carbooking', array ('status' => '0') );
                    break;

                case 'excursionbooking':
                    $this->_ci->db->where ( 'id', $id )->update ( 'excursionbooking', array ('status' => '1') );
                    break;

                case 'tourbooking':
                    $this->_ci->db->where ( 'id', $id )->update ( 'tourbooking', array ('status' => '1') );
                    break;
            }

            //Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).
            return $this->_ci->db->affected_rows ();
        }

    }

?>
